package fun.hijklmn.magic.image.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import fun.hijklmn.magic.common.vo.req.PageVO;
import fun.hijklmn.magic.common.vo.resp.PageResultVO;
import fun.hijklmn.magic.image.common.pojo.Image;
import fun.hijklmn.magic.image.common.vo.req.ImageQueryReqVO;
import fun.hijklmn.magic.image.server.dao.ImageDao;
import fun.hijklmn.magic.image.server.service.ImageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * @Description
 * @Author fs
 * @Date 2021/5/6 16:32
 * @Version 0.0.1-SNAPSHOT
 **/
@Service
public class ImageServiceImpl extends ServiceImpl<ImageDao, Image> implements ImageService {


    @Lazy
    @Autowired
    private ImageService imageService;

    @Override
    public PageResultVO<Image> page(ImageQueryReqVO imageQueryReqVO, PageVO pageVO) {

        QueryWrapper<Image> queryWrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(imageQueryReqVO.getId())) {
            queryWrapper.eq("id", imageQueryReqVO.getId());
        }

        if (StringUtils.isNotEmpty(imageQueryReqVO.getShortCode())) {
            queryWrapper.eq("short_code", imageQueryReqVO.getShortCode());
        }

        if (StringUtils.isNotEmpty(imageQueryReqVO.getTag())) {
            queryWrapper.like("tag", "%" + imageQueryReqVO.getTag() + "%");
        }

        if (StringUtils.isNotEmpty(imageQueryReqVO.getTitle())) {
            queryWrapper.like("title", "%" + imageQueryReqVO.getTitle() + "%");
        }

        if (null != imageQueryReqVO.getStatus()) {
            queryWrapper.eq("status", imageQueryReqVO.getStatus());
        }

        int count = imageService.count(queryWrapper);

        if (count > 0) {
            IPage<Image> page = new Page<>();
            page.setCurrent(pageVO.getCurrentPage());
            page.setSize(pageVO.getPageSize());
            IPage<Image> page1 = imageService.page(page, queryWrapper);
            return new PageResultVO<Image>(pageVO, count, page1.getRecords());
        }

        return new PageResultVO<Image>(pageVO, 0, Lists.newArrayList());

    }

}
